import math
from collections import defaultdict

n = int(input())
l = list(map(int, input().split()))
c = list(map(int, input().split()))
dp = defaultdict(lambda: float('inf'))
# 初始化
for i in range(n):
    dp[l[i]] = min(dp[l[i]], c[i])
# dp
for i in range(n):
    keys = list(dp.keys())
    for j in keys:
        dp[math.gcd(j, l[i])] = min(dp[math.gcd(j, l[i])], dp[j] + c[i])
if dp[1] == float('inf'):
    print(-1)
else:
    print(dp[1])
